//
//  JiFangDianGuiViewController.m
//  Qihuo
//
//  Created by Vencoo on 13-12-5.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//

#import "JiFangDianGuiViewController.h"
#import "JiFangDatas.h"
@interface JiFangDianGuiViewController ()
{
    NSDictionary *voltageDic;
    NSDictionary *currentDic;
    NSDictionary *temperatureDic;
    NSString *mystr1;
    NSString *mystr2;
    NSString *mystr3;
    NSString *resultStr;
    
    NSMutableArray *childTFs;
    NSMutableArray *checkIds;
    NSMutableArray *sendControls;
}
@end

@implementation JiFangDianGuiViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.title = @"机房电柜巡检表";
    [super viewDidLoad];
    self.sendUrl=[NSString stringWithFormat:addCheckEEResult];
    NSArray *queryDataArr = @[
    @{@"id": @"1",@"floorname":@"28F机房",@"rfid":@"<#string#>"},
    @{@"id": @"2",@"floorname":@"27F卫星机房",@"rfid":@"<#string#>"},
    @{@"id": @"3",@"floorname":@"9F机房",@"rfid":@"<#string#>"},
    @{@"id": @"4",@"floorname":@"4F主机房",@"rfid":@"<#string#>"},
    @{@"id": @"5",@"floorname":@"4F辅助机房",@"rfid":@"<#string#>"},
    @{@"id": @"6",@"floorname":@"2F技术公司机房",@"rfid":@"<#string#>"},
    @{@"id": @"7",@"floorname":@"1F中控",@"rfid":@"<#string#>"},
    @{@"id": @"8",@"floorname":@"银行区",@"rfid":@"<#string#>"}];
    self.floorNames = [NSMutableArray array];
    self.floorIds = [NSMutableArray array];
    for (NSDictionary *dic in queryDataArr)
    {
        [self.floorNames addObject:[dic valueForKey:@"floorname"]];
        [self.floorIds addObject:[dic valueForKey:@"id"]];
        [self.sendFloorDatas addObject:@""];
        [self.xunJianCellInfos addObject:@{@"hidden":@"1",@"floorId":[dic valueForKey:@"id"] }];
    }
    
    [self initData];
    [self showUIWithArray:self.allShowDataArr andFloorId:@"1"];
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.myTableView selectRowAtIndexPath:index animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    
}
- (void)initData
{
    mystr1 = [NSString string];
    mystr2 = [NSString string];
    mystr3 = [NSString string];
    
    
    voltageDic = @{@"childName":@"输入电压（V）",@"grandChildNames": @[@"AB相：",@"BC相：",@"CA相："],@"childId":@"1"};
    currentDic = @{@"childName": @"电流（A）",@"grandChildNames":@[@"A相：",@"B相：",@"C相："],@"childId":@"2"};
    temperatureDic = @{@"childName": @"柜内温度（˚C）",@"grandChildNames":@"",@"childId":@"3"};
    self.allShowDataArr = @[voltageDic,currentDic,temperatureDic];
}
- (void)showUIWithArray:(NSArray *)array andFloorId:(NSString *)floorId
{
    [self clearAll];
    BOOL hasReserved = NO;
    UIImage *textImage = [UIImage imageNamed:@"输入框底"];
    NSArray *showArr = [NSArray array];
    
    self.floorIdQuanju = floorId;

    switch ([floorId integerValue])
    {
        case 1:
        {
            NSDictionary *UPSDic = @{@"checkID": @"1",@"checkName":@"UPS：",@"childArray":@[voltageDic,currentDic,temperatureDic]};
            NSDictionary *oneThreeAir = @{@"checkID": @"2",@"checkName":@"1#、3#空调：",@"childArray":@[voltageDic,currentDic,temperatureDic]};
            NSDictionary *twoAir = @{@"checkID": @"3",@"checkName":@"2#空调：",@"childArray":@[voltageDic,temperatureDic]};
            showArr = @[UPSDic,oneThreeAir,twoAir];
        }
            break;
        case 2:
        {
            NSDictionary *UPSDic = @{@"checkID": @"4",@"checkName":@"UPS：",@"childArray":@[voltageDic,currentDic,temperatureDic]};
            showArr = @[UPSDic];
        }
            break;
        case 3:
        {
            NSDictionary *UPSDic = @{@"checkID": @"5",@"checkName":@"UPS：",@"childArray":@[currentDic,temperatureDic]};
            showArr = @[UPSDic];
        }
            break;
        case 4:
        {
            NSDictionary *UPSDic = @{@"checkID": @"6",@"checkName":@"UPS：",@"childArray":@[voltageDic,currentDic,temperatureDic]};
            NSDictionary *airCondition = @{@"checkID": @"7",@"checkName":@"空调：",@"childArray":@[voltageDic,currentDic,temperatureDic]};
            NSDictionary *oneDic = @{@"checkID": @"8",@"checkName":@"1#有源滤波器：",@"childArray":@[temperatureDic]};
            NSDictionary *twoDic = @{@"checkID": @"9",@"checkName":@"2#有源滤波器：",@"childArray":@[temperatureDic]};
            showArr = @[UPSDic,airCondition,oneDic,twoDic];
        }
            break;
        case 5:
        {
            NSDictionary *UPSdic = @{@"checkID": @"10",@"checkName":@"UPS：",@"childArray":@[voltageDic,currentDic,temperatureDic]};
            showArr = @[UPSdic];
        }
            break;
        case 6:
        {
            NSDictionary *UPSdic = @{@"checkID": @"11",@"checkName":@"电柜：",@"childArray":@[voltageDic,currentDic,temperatureDic]};
            showArr = @[UPSdic];
        }
            break;
        case 7:
        {
            NSDictionary *UPSdic = @{@"checkID": @"12",@"checkName":@"UPS：",@"childArray":@[voltageDic,currentDic,temperatureDic]};
            showArr = @[UPSdic];
        }
            break;
        case 8:
        {
            NSDictionary *UPSdic = @{@"checkID": @"13",@"checkName":@"2FA插接箱、1FA电柜：",@"childArray":@[voltageDic,currentDic,temperatureDic]};
            showArr = @[UPSdic];
        }
            break;
            
        default:
            break;
    }
    id receiveFloorObj = [self.sendFloorDatas objectAtIndex:[self.floorIds indexOfObjectss:self.floorIdQuanju]];
    if (![receiveFloorObj isKindOfClass:[NSString class]])
    {
        self.sendCheckDatas = [receiveFloorObj objectForKey:@"checkData"];
        hasReserved = 1;
    }
    else
    {
        hasReserved = 0;
        self.sendCheckDatas = [NSMutableArray array];
        checkIds = [NSMutableArray array];
        for (int i = 0; i < showArr.count; i ++)
        {
            NSDictionary *sendDic = @{@"checkId": [NSString stringWithFormat:@"%d",i+1],@"childData":@[@{@"childId": @"1",@"result": @""},@{@"childId": @"2",@"result": @""},@{@"childId": @"3",@"result": @""}]};
            [self.sendCheckDatas addObject:sendDic];
            [checkIds addObject:[NSString stringWithFormat:@"%d",i+1]];
        }
    }
    childTFs = [NSMutableArray array];
    sendControls = [NSMutableArray array];
    NSMutableArray *mChildIDS = [NSMutableArray array];
    for (int i = 0; i < showArr.count; i ++)
    {
        [mChildIDS removeAllObjects];
        NSDictionary *dataDic = [showArr objectAtIndex:i];
        NSString *checkId = [dataDic valueForKey:@"checkID"];
        NSMutableArray *childDataArr = [NSMutableArray array];
        if (hasReserved)
        {
            childDataArr = self.sendCheckDatas[i][@"childData"];
        }
        else
        {
            childDataArr = [NSMutableArray arrayWithArray: @[
                                        @{@"childId": @"1", @"result": @""},
                                        @{@"childId": @"2", @"result": @""},
                                        @{@"childId": @"3", @"result": @""}
                                                             ]];
        }

        NSString *checkName = [dataDic valueForKey:@"checkName"];
        NSArray *childArray = [dataDic valueForKey:@"childArray"];
        [checkIds addObject:checkId];
        NSMutableArray *checkControl = [NSMutableArray array];
        
        UILabel *checkNameLb = [UILabel new];
        checkNameLb.text = checkName;
        checkNameLb.font = [UIFont systemFontOfSize:35];
        CGSize mySz = [checkNameLb.text sizeWithFont:checkNameLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        checkNameLb.frame = CGRectMake(10, [[childTFs lastObject]frame].origin.y + [[childTFs lastObject]frame].size.height + interval111, mySz.width, mySz.height);
        [self.backScrollV addSubview:checkNameLb];
        [self.controlWillClears addObject:checkNameLb];
        NSMutableArray *idchilds = [NSMutableArray array];
        JiFangDatas *jifang = [JiFangDatas JiFangWithFloorIdIndex:[floorId integerValue]-1 AndCheckIdIndex:i];
        for (int j = 0; j < childArray.count; j++)
        {
            NSDictionary *childDataDic = [childArray objectAtIndex:j];
            id grandChildArr = [childDataDic objectForKey:@"grandChildNames"];
            NSString *childId = [childDataDic objectForKey:@"childId"];
            [mChildIDS addObject:childId];
            UILabel *childLb = [UILabel new];
            childLb.text = [childDataDic valueForKey:@"childName"];
            childLb.font = [UIFont systemFontOfSize:25];
            mySz = [childLb.text sizeWithFont:childLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
            if (j == 0)
            {
                childLb.frame = CGRectMake(60, checkNameLb.frame.size.height + checkNameLb.frame.origin.y + interval111, mySz.width, mySz.height);
            }
            else
            {
                childLb.frame = CGRectMake(60, [[childTFs lastObject]frame].origin.y + [[childTFs lastObject]frame].size.height + interval111 * 2, mySz.width, mySz.height);
            }
            [self.backScrollV addSubview:childLb];
            [self.controlWillClears addObject:childLb];
//            [childDataArr addObject:@{@"childId":childId,@"result":@""}];
            
            if ([grandChildArr isKindOfClass:[NSString class]])
            {
                UITextField *childTF = [[UITextField alloc]initWithFrame:CGRectMake(200, childLb.frame.origin.y + childLb.frame.size.height + interval111, 180, mySz.height)];
                childTF.background = textImage;
                childTF.text = [self.sendCheckDatas[i][@"childData"]lastObject][@"result"];
                childTF.keyboardType = UIKeyboardTypeNumberPad;
                childTF.delegate = self;
                [self.backScrollV addSubview:childTF];
                [childTFs addObject:childTF];
                [self.controlWillClears addObject:childTF];
                NSArray *tfs = @[childTF];
                [checkControl addObject:tfs];
                
                UILabel *childLb = [[UILabel alloc]initWithFrame:CGRectMake(childTF.frame.origin.x + childTF.frame.size.width + 10, childTF.frame.origin.y, 220, childTF.frame.size.height)];
                childLb.text = [NSString stringWithFormat:@"（参考值：%@）",jifang.temperatureStr];
                childLb.textColor = [UIColor redColor];
                [self.backScrollV addSubview:childLb];
                [self.controlWillClears addObject:childLb];
            }
            else
            {
                NSArray *grandChildArrar = (NSArray *)grandChildArr;
                NSMutableArray *tfs = [NSMutableArray array];
                NSString *text = self.sendCheckDatas[i][@"childData"][j][@"result"];
                NSArray *textArr = [text componentsSeparatedByString:@","];
                for (int K = 0; K < grandChildArrar.count; K++)
                {
                    UILabel *grandChildLb = [UILabel new];
                    grandChildLb.text = [grandChildArrar objectAtIndex:K];
                    grandChildLb.font = [UIFont systemFontOfSize:25];
                    mySz = [grandChildLb.text sizeWithFont:grandChildLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
                    if (K == 0)
                    {
                        grandChildLb.frame = CGRectMake(childLb.frame.origin.x + 50, childLb.frame.size.height + childLb.frame.origin.y + interval111, mySz.width, mySz.height);
                    }
                    else
                    {
                        grandChildLb.frame = CGRectMake(childLb.frame.origin.x + 50, [[childTFs lastObject]frame].origin.y + [[childTFs lastObject]frame].size.height + interval111, mySz.width, mySz.height);
                    }
                    [self.backScrollV addSubview:grandChildLb];
                    [self.controlWillClears addObject:grandChildLb];
                    
                    UITextField *childTF = [[UITextField alloc]initWithFrame:CGRectMake(200, grandChildLb.frame.origin.y, 180, mySz.height)];
                    if (hasReserved)
                    {
                        childTF.text = textArr[K];
                    }
                    childTF.keyboardType = UIKeyboardTypeNumberPad;
                    childTF.background = textImage;
                    childTF.delegate = self;
                    [self.backScrollV addSubview:childTF];
                    [childTFs addObject:childTF];
                    [self.controlWillClears addObject:childTF];
                    [tfs addObject:childTF];
                    UILabel *childLb = [[UILabel alloc]initWithFrame:CGRectMake(childTF.frame.origin.x + childTF.frame.size.width + 10, childTF.frame.origin.y, 220, childTF.frame.size.height)];
                    if ([childId isEqualToString:@"1"])
                    {
                        childLb.text = [NSString stringWithFormat:@"（参考值：%@）",jifang.voltageStr];
                    }
                    else if ([childId isEqualToString:@"2"])
                    {
                        childLb.text = [NSString stringWithFormat:@"（参考值：%@）",jifang.currentStr];
                    }
                    childLb.textColor = [UIColor redColor];
                    [self.backScrollV addSubview:childLb];
                    [self.controlWillClears addObject:childLb];
                }
                [checkControl addObject:tfs];
            }
            idchilds = [NSMutableArray arrayWithArray:mChildIDS];
        }
        [sendControls addObject:@{@"checkId": checkId,@"checkControls":checkControl,@"childData":childDataArr,@"childIds":idchilds}];
    }
    [self addCommentKuang:@""];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.myTableView) {
        return self.floorNames.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if (tableView == self.myTableView)
    {
        self.mycell.textLabel.text = [self.floorNames objectAtIndex:indexPath.row];
        return self.mycell;
    }
    return nil;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self hiddenAll];
    if (tableView == self.myTableView)
    {
        [self showUIWithArray:self.allShowDataArr andFloorId:[self.floorIds objectAtIndex:indexPath.row]];
    }
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    for (int i = 0; i < sendControls.count; i++)
    {
        NSDictionary *controlDic = [sendControls objectAtIndex:i];
        NSString *checkId = [controlDic valueForKey:@"checkId"];
        NSMutableArray *controls = [controlDic objectForKey:@"checkControls"];
        NSMutableArray *childDataArr = [controlDic objectForKey:@"childData"];
        NSMutableArray *childIds = [controlDic objectForKey:@"childIds"];
        for (int k = 0; k < controls.count; k++)
        {
            NSArray *tfs = [controls objectAtIndex:k];
            NSString *childID = [childIds objectAtIndex:k];
            for (int j = 0; j < tfs.count; j++)//child
            {
                id tf = [tfs objectAtIndex:j];
                if (textField == tf)
                {
                    if (tfs.count == 1)
                    {
                        resultStr = [textField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                    }
                    else if (tfs.count == 3)
                    {
                        switch (j)
                        {
                            case 0:
                            {
                                mystr1 = textField.text;
                            }
                                break;
                            case 1:
                            {
                                mystr2 = textField.text;
                            }
                                break;
                            case 2:
                            {
                                mystr3 = textField.text;
                            }
                                break;
                                
                            default:
                                break;
                        }
                        resultStr = [NSString stringWithFormat:@"%@,%@,%@",mystr1,mystr2,mystr3];
                    }
                    NSDictionary *dic = @{@"childId": childID,@"result":resultStr};
                    int index = [@[@"1",@"2",@"3"]indexOfObject:childID];
                    [childDataArr replaceObjectAtIndex:index withObject:dic];
                    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:checkId,@"checkId",childDataArr,@"childData",nil];
                    [self.sendCheckDatas replaceObjectAtIndex:i withObject:dic1];
                    NSLog(@"%@",self.sendCheckDatas);
                }
            }
        }
    }
}
#pragma mark - end
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
